// releasePage/pages/addQuestion/addQuestion.js
import throttle from '../../../utils/throttle.js';
const app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        id: '',
        description: '',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (app.globalData.isIpx) {
            this.setData({
                ipxBottom: 34,
            });
        }
        if (options.id) {
            this.setData({
                id: options.id,
                description: options.content,
            });
        }
        wx.setNavigationBarTitle({
            title: '提问·' + app.globalData.addressBack,
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    back() {
        wx.navigateBack({
            detal: 1,
        });
    },
    description(e) {
        this.setData({
            description: e.detail.value,
        });
        app.numLimitTip(e.detail.value.length, 100);
    },
    submit: throttle(
        function (e) {
            let _that = this;
            if (app.visitorPublish()) return; //游客发布限制
            if (_that.data.description.length === 0) return;
            if (!_that.data.id) {
                wx.request({
                    url:
                        app.globalData.rootPath +
                        `cjMap/createCityQuestion?authId=${app.globalData.authId}`,
                    method: 'POST',
                    header: {
                        'content-type': 'application/json',
                    },
                    data: {
                        delflg: '00', //删除标志 00未删除 01已删除 02待审核10草稿
                        description: _that.data.description,
                        city: app.globalData.addressBack, //城市
                        country: app.globalData.countryBack, //国家
                    },
                    success: function (res) {
                        console.log(res);
                        wx.showToast({
                            title: '录入成功',
                            icon: 'success',
                            duration: 2000,
                        });
                        let pages = getCurrentPages(); //当前页面栈
                        let prevPage = pages[pages.length - 2]; //上一页面
                        let prevPage1 = pages[pages.length - 3];
                        prevPage.refresh();
                        prevPage1.refresh();
                    },
                });
            } else {
                //编辑问题
                wx.request({
                    url:
                        app.globalData.rootPath +
                        `cjMap/editCityQuestion?authId=${app.globalData.authId}`,
                    method: 'POST',
                    header: {
                        'content-type': 'application/json',
                    },
                    data: {
                        description: _that.data.description,
                        id: _that.data.id,
                    },
                    success: function (res) {
                        wx.showToast({
                            title: '录入成功',
                            icon: 'success',
                            duration: 2000,
                        });
                        let pages = getCurrentPages(); //当前页面栈
                        let prevPage = pages[pages.length - 2]; //上一页面
                        let prevPage1 = pages[pages.length - 3];
                        prevPage.refresh();
                        prevPage1.refresh();
                    },
                });
            }

            setTimeout(function () {
                wx.navigateBack({
                    delta: 1, // 返回上一级页面。
                });
            }, 2000);
        },
        4000,
        {
            trailing: false,
        }
    ),
});
